<script setup>
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

const currentTime = ref(new Date())
const isHomePage = computed(() => route.path === '/home')

let timer = null

onMounted(() => {
  timer = setInterval(() => {
    currentTime.value = new Date()
  }, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})

const formatTime = (date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')

  return `${year}年${month}月${day}日-${hours}时${minutes}分${seconds}秒`
}

const goToHome = () => {
  router.push('/')
}
</script>

<template>
  <div id="app">
    <div class="header" v-if="isHomePage">
      <h1>数据可视化</h1>
      <div class="nav-links">
        <a href="javascript:;" @click="goToHome">返回首页</a>
      </div>
      <div class="showTime">
        当前时间：{{ formatTime(currentTime) }}
      </div>
    </div>
    <router-view />
  </div>
</template>

<style scoped lang="scss">
#app {
  background: url(./assets/bg.jpg) no-repeat center center;
  background-size: cover;
  background-attachment: fixed;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;

  .header {
    box-sizing: border-box;
    position: relative;
    background: url(./assets/head_bg.png) no-repeat;
    background-size: 100% 100%;
    color: #fff;
    height: 100px;
    min-height: 100px;
    max-height: 100px;
    padding: 0 15px;
    flex-shrink: 0;

    h1 {
      height: 80px;
      font-size: 2.4rem;
      /* 使用rem单位 */
      color: rgba(255, 255, 255, 0.87);
      text-align: center;
      line-height: 80px;
      margin: 0;
    }

    .nav-links {
      position: absolute;
      top: 0;
      left: 30px;
      line-height: 75px;
      z-index: 10;

      a {
        color: rgba(25, 186, 139, 0.9);
        font-size: 1.1rem;
        text-decoration: none;
        padding: 5px 10px;
        border-radius: 4px;
        transition: all 0.3s ease;
        
        &:hover {
          color: #fff;
          background-color: rgba(25, 186, 139, 0.7);
        }
      }
    }

    .showTime {
      position: absolute;
      top: 0;
      right: 30px;
      color: rgba(255, 255, 255, 0.7);
      font-size: 1.25rem;
      /* 使用rem单位 */
      line-height: 75px;
    }
  }
}

/* 为 home 组件添加样式 */
:deep(.home-page) {
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 100px);
}
/* 响应式适配 */
/* 大屏幕 (1200px 及以上) */
@media (min-width: 1200px) {
  #app {
    .header {
      h1 {
        font-size: 3.2rem;
      }
      .showTime {
        font-size: 1.25rem;
      }
    }
  }
}
/* 中屏幕 (992px - 1199px) */
@media (min-width: 992px) and (max-width: 1199px) {
  #app {
    .header {
      h1 {
        font-size: 2.4rem;
      }
      .showTime {
        font-size: 1.1rem;
      }
    }
  }
}
/* 小屏幕 (768px - 991px) */
@media (min-width: 768px) and (max-width: 991px) { 
  #app {
    .header {
      h1 {
        font-size: 2rem;
      }
      .showTime{
        font-size: 1rem;
      }
    }
  }
}
</style>
